<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 14:46
 */

class Involved_ProductModel extends BaseModel
{
    protected $table = 'product';

    /**************************API端***********************/




    /**
     * 删除库存商品
     * @param $product_id
     * @param $supplier_id
     * @return mixed
     */
    public function DeleteStoreProduct($product_id, $supplier_id)
    {
        return DB::table($this->table)
            ->where('product_id', $product_id)
            ->where('supplier_id', $supplier_id)
            ->delete();

    }

    /**
     * 获取商品名称
     * @param $product_id
     * @return string
     */
    public function getNameById($product_id)
    {
        $product_Info = DB::table($this->table)
            ->select('product_name')
            ->where('product_id', $product_id)
            ->first();
        if (empty($product_Info))
            return '';

        return $product_Info->product_name;
    }

    /**
     * 所有产品列表
     * @return info
     */
    public function GetProductList()
    {
        return DB::table($this->table)
            ->select('product_name', 'spec', 'place', 'stock', 'shop_price', 'product_img1', 'level', 'classes')
            ->get();
    }

    /**
     * 根据ID获取产品详情
     * @param $product_id
     * @return mixed
     */
    public function GetProductById($product_id)
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id,sc_brand.brand_name as product_brand,sc_product.product_spec,sc_storage.storage_place,sc_product.product_price,sc_storage.storage_name as storage,sc_factory.factory_name as factory,sc_material.m_name as product_material,sc_product.useable_stock,sc_product.piece_weight')
            ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
            ->leftjoin('storage', 'product.storage', '=', 'storage.storage_id')
            ->leftjoin('factory', 'product.factory', '=', 'factory.factory_id')
            ->where('product.product_id', $product_id)
            ->first();
    }

    /**
     * 产品详情
     * @param $product_id
     * @author fyl
     * @date 2016-1-25
     * @return mixed
     */
    public function GetProductDetail($product_id)
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id,sc_brand.brand_name as product_brand,sc_product.product_spec,sc_storage.storage_place,sc_product.product_price,sc_storage.storage_name as storage,sc_factory.factory_name as factory,sc_material.m_name as product_material,sc_product.useable_stock,sc_product.piece_weight')
            ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
            ->leftjoin('storage', 'product.storage', '=', 'storage.storage_id')
            ->leftjoin('factory', 'product.factory', '=', 'factory.factory_id')
            ->where(['product.product_id' => $product_id])
            ->first();
    }

    public function GetProductBoard()
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id, sc_product.product_name,sc_product.price, sc_product.stock,
            sc_product.product_img,sc_product.is_up,sc_cate1.cate_name AS category,sc_cate2.cate_name AS classes,
            sc_cate3.cate_name AS quality, sc_product_brand.brand_name, sc_supplier.supplier_name')
            ->leftJoin('product_cate as sc_cate1', 'cate1.cate_id', '=', 'product.category')
            ->leftJoin('product_cate as sc_cate2', 'cate2.cate_id', '=', 'product.classes')
            ->leftJoin('product_cate as sc_cate3', 'cate3.cate_id', '=', 'product.quality')
            ->leftJoin('product_brand', 'product_brand.brand_id', '=', 'product.brand_id')
            ->leftJoin('supplier', 'supplier.supplier_id', '=', 'product.supplier_id')
            ->get();
    }


    /**
     * 上下架产品列表
     * @param $supplier_id
     * @param $op
     * @param $page
     * @return mixed
     * @author fyl
     * @date 2016-5-13
     */
    public function GetManageProduct($supplier_id, $op, $page, $status)
    {
        if ($status != 2) {
            $offset = ($page - 1) * HostEnum::PAGE_NUM;
            return DB::table($this->table)
                ->select('product_id', 'product_name', 'place', 'price', 'product_img', 'is_up', 'created', 'stock', 'spec')
                ->where(['is_up' => $op, 'supplier_id' => $supplier_id])
                ->where('status', 1)
                ->orderBy('created', 'desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
        } else {
            $offset = ($page - 1) * HostEnum::PAGE_NUM;
            return DB::table($this->table)
                ->select('product_id', 'product_name', 'place', 'price', 'product_img', 'is_up', 'created', 'stock', 'spec')
                ->where(['is_up' => $op, 'supplier_id' => $supplier_id])
                ->where('status', '!=', 1)
                ->orderBy('created', 'desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
        }

    }

    /**
     * 首页产品列表
     * @return mixed
     * @author fyl
     * @date 2016-5-1
     */
    public function GetIndexData()
    {
        return DB::table($this->table)
            ->select('product_name', 'thumb_img1', 'spec', 'level', 'stock', 'shop_price')
            ->where('stock', '!=', 0)
            ->where('is_up', '!=', 0)
            ->where('status', 1)
            ->get();
    }

    /**
     * 首页展示列表
     * @param $inWhere
     * @param $page
     * @author fyl
     * @date 2016-5-21
     * @return mixed
     */
    public function GetSelfSupportList($inWhere, $page)
    {
        #分页
        $page = empty($page) ? 1 : $page;
        $offset = ($page - 1) * HostEnum::PAGE_NUM;

        #查询语句
        $oQuery = DB::table($this->table)->select('product_id', 'product_name', 'product_img', 'spec', 'price', 'supplier_id', 'created', ''); //字段
        /*if($inWhere == 'index') //条件
            $oQuery->where(['is_index' => ProductEnum::PRODUCT_INDEX_YES, 'is_self_support' => ProductEnum::PRODUCT_INDEX_YES]);*/
        /* $oQuery->orderBy('created','desc');*/
        $oQuery->where(['is_up' => 1, 'status' => 1]);
        $oQuery->orderBy('created', 'desc');
        $info = $oQuery->skip($offset)->take(HostEnum::PAGE_NUM)->get(); //结果

        return $info;

    }

    /**
     * 自营产品列表
     * @param $service_type
     * @param $page
     * @author fyl
     * @date 2016-6-22
     * @return mixed
     */
    public function GetSelfSupportProduct($service_type, $page)
    {
        if ($page == 1) {
            $offset = ($page - 1);
        } else {
            $offset = ($page - 1) * HostEnum::PAGE_NUM;
        }
        return DB::table($this->table)
            ->select('product_id', 'product_name', 'product_img', 'level', 'spec', 'place', 'stock', 'price', 'category')
            ->where(['is_self_support' => 1, 'channel_id' => $service_type])
            ->where('stock', '!=', 0)
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();

    }

    /**
     * 首页推荐商品列表
     * @author fyl
     * @date 2016-8-29
     * @return mixed
     */
    public function GetindexList($limit)
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id,sc_brand.brand_name as product_brand,sc_product.product_spec,sc_product.product_price,sc_factory.factory_name as factory,sc_material.m_name as product_material')
            ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
            ->leftjoin('factory', 'product.factory', '=', 'factory.factory_id')
            ->where('is_index', 1)
            ->orderBy('product_id', 'desc')
            ->take($limit)
            ->get();
    }

    /**
     * 首页新品商品列表
     * @author fyl
     * @date 2016-8-29
     * @return mixed
     */
    public function GetNewindexList($limit)
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id,sc_brand.brand_name as product_brand,sc_product.product_spec,sc_storage.storage_city,sc_product.product_price,sc_storage.storage_name as storage,sc_factory.factory_name as factory,sc_material.m_name as product_material,sc_product.useable_stock,sc_product.piece_weight')
            ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
            ->leftjoin('storage', 'product.storage', '=', 'storage.storage_id')
            ->leftjoin('factory', 'product.factory', '=', 'factory.factory_id')
            ->orderBy('created', 'desc')
            ->take($limit)
            ->get();
    }

    public function Shopping_Index1($category)
    {
        $db = DB::table($this->table);
        if (!empty($category)) {
            $db->where('category', $category);
        }
        return $db
            ->select('product_id', 'product_name', 'product_img', 'level', 'spec', 'place', 'stock', 'price', 'unit', 'category')
            ->where('stock', '!=', 0)
            //   ->where(['is_self_support'=> 1])
            ->where('is_up', '!=', 0)
            ->where('status', '=', 1)
            ->orderBy('product_id', 'desc')
            ->get();


    }

    public function Shopping_Index2($quality)
    {
        $db = DB::table($this->table);
        if (!empty($quality)) {
            $db->where('quality', $quality);
        }
        return $db
            ->select('product_id', 'product_name', 'product_img', 'level', 'spec', 'place', 'stock', 'price', 'unit', 'category')
            ->where('stock', '!=', 0)
            //   ->where(['is_self_support'=> 1])
            ->where('is_up', '!=', 0)
            ->where('status', '=', 1)
            ->orderBy('product_id', 'desc')
            ->get();


    }

    public function ShoppingNull()
    {
        $db = DB::table($this->table);
        #查询
        $goods = $db
            ->select('product_id', 'product_name', 'product_img', 'level', 'spec', 'place', 'stock', 'price', 'unit', 'category')
            ->where('stock', '!=', 0)
            //    ->where(['is_self_support'=> 1])
            ->where('is_up', '!=', 0)
            ->where('status', '=', 1)
            ->orderBy('product_id', 'desc')
            ->get();
        //    ->paginate(12);
        return $goods;

    }

    #关键字搜索
    public function SelectKeyword($name)
    {
        return DB::table($this->table)
            ->select('product_id', 'product_name', 'product_img', 'level', 'spec', 'place', 'stock', 'price', 'unit', 'category')
            ->where('stock', '!=', 0)
            //    ->where(['is_self_support'=> 1])
            ->where('product_name', 'like', "%{$name}%")
            ->where('is_up', '!=', 0)
            ->where('status', '=', 1)
            ->orderBy('product_id', 'desc')
            //   ->paginate(12);
            ->get();
    }


    /**
     * 订单产品列表
     * @param $product_id
     * @return mixed
     * @author alice
     * @date 2016-2-19
     */
    public function GetOrderProductList($product_id)
    {
        return DB::table($this->table)
            ->select('product_id', 'product_name', 'product_img', 'spec', 'price', 'place')
            ->where(['product_id' => $product_id])
            ->get();
    }

    public function GetOrderProductList1($product_id, $order_id)
    {
        return DB::table($this->table)
            ->select('product.product_id', 'product.product_name', 'product.product_img', 'product.spec', 'product.price', 'product.place', 'product.unit', 'product.storage')
            ->LeftJoin('order_product', 'product.product_id', '=', 'order_product.product_id')
            ->where(['product.product_id' => $product_id, 'order_product.order_id' => $order_id])
            ->get();
    }

    public function GetOrderProductList2($product_id)
    {
        return DB::table($this->table)
            ->select('product_id', 'product_name', 'product_img', 'description')
            ->where(['product_id' => $product_id])
            ->get();
    }

    /**************************PC端***********************/

    /**
     * 产品列表
     * @author fyl
     * @date 2016-8-16
     * @return mixed
     */
    public function ShowGoodsList()
    {
        return DB::table($this->table)
            ->selectRaw('sc_brand.brand_name,sc_material.m_name,sc_product.product_id,sc_product.product_spec,sc_product.product_price,sc_product.piece_weight,sc_product.useable_stock')
            ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
            ->orderBy('product.product_id', 'desc')
            ->where('status', 2)
            //->orderBy('product.length','asc')
            ->get();
    }

    /**
     * 产品详情
     * @param $id
     * @author FYL
     * @date 2016-8-17
     * @return mixed
     */
    public function GetGoodsInfo($id)
    {
        return DB::table($this->table)->where('product_id', $id)->first();
    }

    /**
     * 后台获取产品列表
     * @author fyl
     * @date 2016-04-04
     */
    public function AdminProductList()
    {
        return DB::table($this->table)
            ->selectRaw('sc_admin.real_name as add_people,sc_brand.brand_name,sc_material.m_name,sc_product.product_id, sc_product.product_spec,sc_product.product_price,sc_product.piece_weight,sc_product.locking_stock,sc_product.useable_stock,sc_product.real_stock,sc_product.duty_people,sc_product.status')
            ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
            ->leftJoin('admin', 'product.add_people', '=', 'admin.id')
            ->orderBy('product.product_id', 'desc')
            //->orderBy('product.length','asc')
            ->get();
    }

    /**
     * 获取产品审核列表
     * @author fyl
     * @date 2016-04-04
     */
    public function ProductExamineList()
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id, sc_product.product_name, sc_product.product_img,sc_supplier.supplier_name,
     sc_cate1.cate_name as category,sc_product.status')
            ->leftJoin('product_cate as sc_cate1', 'cate1.cate_id', '=', 'product.category')
            ->leftJoin('supplier', 'supplier.supplier_id', '=', 'product.supplier_id')
            ->where('product.status', 2)
            ->orderBy('product.created', 'desc')
            ->get();
    }


    /**
     * 根据ID获取产品详情(审核)
     * @return mixed
     */
    public function AdminGetProductById($id)
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_brand,sc_product.product_material,sc_product.product_id,sc_product.product_spec,sc_product.product_price,sc_product.piece_weight,sc_product.locking_stock,sc_product.useable_stock,sc_product.real_stock,sc_product.storage,sc_product.factory,sc_product.duty_people,sc_product.remarks,sc_product.category')
            ->leftJoin('admin', 'product.add_people', '=', 'admin.id')
            ->where('product.product_id', $id)
            ->first();
    }

    /**
     * 根据brand_ID获取该品名下的商品
     * @return mixed
     */
    public function GetProductBrand($id)
    {
        return DB::table($this->table)
            ->select('product_id')
            ->where('product_brand', $id)
            ->first();
    }

    /**
     * 根据Material_ID获取该材质下的商品
     * @return mixed
     */
    public function GetProductMaterial($id)
    {
        return DB::table($this->table)
            ->select('product_id')
            ->where('product_material', $id)
            ->first();
    }

    //加库
    public function lock($number, $product_id)
    {
        return DB::table($this->table)
            ->where('product_id', $product_id)
            ->increment('locking_stock', $number);
    }

    public function lock1($number, $product_id)
    {
        return DB::table($this->table)
            ->where('product_id', $product_id)
            ->decrement('useable_stock', $number);
    }

    //减库
    public function cancellock($number, $product_id)
    {
        return DB::table($this->table)
            ->where('product_id', $product_id)
            ->increment('useable_stock', $number);
    }

    public function cancellock1($number, $product_id)
    {
        return DB::table($this->table)
            ->where('product_id', $product_id)
            ->decrement('locking_stock', $number);
    }

    /**
     * @param $id
     * @return array
     *获取推荐商品详情
     */
    public function AdProductInfo($id)
    {
        return DB::table($this->table)
            ->select('product_id', 'product_name', 'unit', 'price', 'spec', 'product_img')
            ->whereIn('product_id', $id)
            ->get();
    }

    /**
     * @param $id
     * @return array
     *获取商品可用数量
     */
    public function GetProductUseableStock($id)
    {
        return DB::table($this->table)
            ->select('useable_stock')
            ->where('product_id', $id)
            ->first();
    }



    // *************************************         newpc        ************************************************************

    /**
     * 商城模块
     * @param $page
     * @return mixed
     * @author 刘玉超
     * @date 2016-09-07
     */
    public function Shopping(){
        return DB::table($this->table)
            ->select('product_id','product_brand','product_material','product_spec','factory'
                ,'storage','piece_weight','product_price','useable_stock', 'brand_name', 'm_name', 'factory_name','storage_name')
            ->join('material', 'material.m_id', '=', 'product.product_material')
            ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->join('factory', 'factory.factory_id', '=', 'product.factory')
            ->join('storage', 'storage.storage_id', '=', 'product.storage')
            ->orderBy('created','desc')
            ->paginate(20);

    }
    public function ShoppingArray1(){
        return DB::table($this->table)
            ->select('product_id','product_brand','product_material','product_spec','factory'
                ,'storage','piece_weight','product_price','useable_stock', 'brand_name', 'm_name', 'factory_name','storage_name')
            ->join('material', 'material.m_id', '=', 'product.product_material')
            ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->join('factory', 'factory.factory_id', '=', 'product.factory')
            ->join('storage', 'storage.storage_id', '=', 'product.storage')
            ->orderBy('created','desc')
            ->get();

    }
    public function ShoppingSelect($paramObj){

        $db = DB::table($this->table);
        //地区
        if (!empty($paramObj->address) && $paramObj->address != '全部' ) {
            $db->where('storage_city', 'like', "%{$paramObj->address}%");
        }

        //品名
        if (!empty($paramObj->brand) && $paramObj->brand !='all' ) {
            $db->where('product_brand',$paramObj->brand);
        }
        //规格
        if (!empty($paramObj->spec)) {
            $db->where('product_spec',$paramObj->spec);
        }
        //材质
        if (!empty($paramObj->material) && $paramObj->material !='all') {
            $db->where('product_material', $paramObj->material);
        }
        //仓库
        if (!empty($paramObj->storage) && $paramObj->storage !='all') {
            $db->where('storage',$paramObj->storage);
        }
        //钢厂
        if (!empty($paramObj->factory) && $paramObj->factory !='all') {
            $db->where('factory',$paramObj->factory);
        }
        //钢厂

        if (!empty($paramObj->product_id)) {

            $db->where('product_id',$paramObj->product_id);
        }
        return  $db
            ->select('product_id','product_brand','product_material','product_spec','factory'
                ,'storage','piece_weight','product_price','useable_stock', 'brand_name', 'm_name', 'factory_name','storage_name')
            ->join('material', 'material.m_id', '=', 'product.product_material')
            ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->join('factory', 'factory.factory_id', '=', 'product.factory')
            ->join('storage', 'storage.storage_id', '=', 'product.storage')
            ->orderBy('created','desc')
            ->paginate(20);

    }
    public function ShoppingArray($paramObj){

        $db = DB::table($this->table);
        //地区
        if (!empty($paramObj->address) && $paramObj->address != '全部' ) {
            $db->where('storage_city', 'like', "%{$paramObj->address}%");
        }

        //品名
        if (!empty($paramObj->brand) && $paramObj->brand !='all' ) {
            $db->where('product_brand',$paramObj->brand);
        }
        //规格
        if (!empty($paramObj->spec)) {
            $db->where('product_spec',$paramObj->spec);
        }
        //材质
        if (!empty($paramObj->material) && $paramObj->material !='all') {
            $db->where('product_material', $paramObj->material);
        }
        //仓库
        if (!empty($paramObj->storage) && $paramObj->storage !='all') {
            $db->where('storage',$paramObj->storage);
        }
        //钢厂
        if (!empty($paramObj->factory) && $paramObj->factory !='all') {
            $db->where('factory',$paramObj->factory);
        }
        //钢厂

        if (!empty($paramObj->product_id)) {

            $db->where('product_id',$paramObj->product_id);
        }
        return  $db
            ->select('product_id','product_brand','product_material','product_spec','factory'
                ,'storage','piece_weight','product_price','useable_stock', 'brand_name', 'm_name', 'factory_name','storage_name')
            ->join('material', 'material.m_id', '=', 'product.product_material')
            ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->join('factory', 'factory.factory_id', '=', 'product.factory')
            ->join('storage', 'storage.storage_id', '=', 'product.storage')
            ->orderBy('created','desc')
            ->get();

    }

    //加可用库存
    public function UpdateUseable($product_id,$number)
    {
        return  DB::table($this->table)
            ->where('product_id',$product_id)
            ->increment('useable_stock', $number);

    }
    //减锁定库存
    public function UpdateLocking($product_id,$number)
    {
        DB::table($this->table)
            ->where('product_id',$product_id)
            ->decrement('locking_stock', $number);
    }
    //减实际库存
    public function UpdateReal($product_id,$number)
    {
        DB::table($this->table)
            ->where('product_id',$product_id)
            ->decrement('real_stock', $number);
    }





   // *************************************          API         ************************************************************
    /**
     * 商城数据
     * @param $page
     * @return mixed
     * @author lzhp
     * @date 2016-09-06
     */
    public function ShopData($page,$city,$category,$factory,$spec,$keywords)
    {
        $db =  DB::table($this->table);
        $db1 = DB::table($this->table);
        $db2 = DB::table($this->table);
        //地区
        if (!empty($city) && $city != 'all') {
            $db->where('storage_city', 'like', "%{$city}%");
        }
        //品类
        if (!empty($category) && $category != 'all') {
            $db ->where('category_name', 'like', "%{$category}%");
        }
        //规格
        if (!empty($spec) && $spec != 'all') {
            $db ->where('product_spec', 'like', "%{$spec}%");
        }
        //钢厂
        if (!empty($factory) && $factory != 'all') {
            $db->where('factory_name', 'like', "%{$factory}%");
        }
        //关键字
        if(!empty($keywords)) {
            $db1->where('brand_name', 'like', "%{$keywords}%");
            //地区
            if (empty($city)) {
                $db1->orwhere('storage_city', 'like', "%{$keywords}%");
            }
            //品类
            if (empty($category)) {
                $db1->orwhere('category_name', 'like', "%{$keywords}%");
            }
            //规格
            if (empty($spec)) {
                $db1->orwhere('product_spec', 'like', "%{$keywords}%");
            }
            //钢厂
            if (empty($factory)) {
                $db1->orwhere('factory_name', 'like', "%{$keywords}%");
            }
        }
        $limit = ($page - 1) * 10;
        if(empty($city)&&empty($category)&&empty($spec)&&empty($factory)&&!empty($keywords)){
            $data = $db2->select('product_id', 'product_price', 'product_spec', 'brand_name', 'm_name', 'factory_name','storage_name','storage_city','category_name','piece_weight','useable_stock')
                ->join('material', 'material.m_id', '=', 'product.product_material')
                ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
                ->join('factory', 'factory.factory_id', '=', 'product.factory')
                ->join('storage', 'storage.storage_id', '=', 'product.storage')
                ->join('category_spec', 'category_spec.id', '=', 'product.category')
                ->where('is_index', 1)
                ->where('brand_name', 'like', "%{$keywords}%")
                ->orwhere('storage_city', 'like', "%{$keywords}%")
                ->orwhere('category_name', 'like', "%{$keywords}%")
                ->orwhere('product_spec', 'like', "%{$keywords}%")
                ->orwhere('factory_name', 'like', "%{$keywords}%")
                ->orderBy('created', 'desc')
                ->skip($limit)->take(10)
                ->get();
        }else{
            $data = $db
                ->select('product_id', 'product_price', 'product_spec', 'brand_name', 'm_name', 'factory_name','storage_name','storage_city','category_name','piece_weight','useable_stock')
                ->join('material', 'material.m_id', '=', 'product.product_material')
                ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
                ->join('factory', 'factory.factory_id', '=', 'product.factory')
                ->join('storage', 'storage.storage_id', '=', 'product.storage')
                ->join('category_spec', 'category_spec.id', '=', 'product.category')
                ->whereExists(function($db1){
                    $db1;
                })
                ->where('is_index', 1)
                ->orderBy('created', 'desc')
                ->skip($limit)->take(10)
                ->get();
        }

        return $data;
    }






    public function ShopDataOne($product_id)
    {
        return DB::table($this->table)
            ->select('product_id', 'product_price', 'product_spec', 'brand_name', 'm_name', 'factory_name','storage_name','storage_city','category_name','piece_weight','useable_stock')
            ->join('material', 'material.m_id', '=', 'product.product_material')
            ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->join('factory', 'factory.factory_id', '=', 'product.factory')
            ->join('storage', 'storage.storage_id', '=', 'product.storage')
            ->join('category_spec', 'category_spec.id', '=', 'product.category')
            ->where('is_index', 1)
            ->where('product_id',$product_id)
            ->orderBy('created', 'desc')
            ->first();
    }

    /**
     * 首页数据
     * @return mixed
     * @author lzhp
     * @date 2016-09-05
     */
    public function IndexData($page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->select('product_id', 'product_price', 'product_spec', 'brand_name', 'm_name', 'factory_name','storage_name','storage_city','category_name','piece_weight')
            ->join('material', 'material.m_id', '=', 'product.product_material')
            ->join('brand', 'brand.brand_id', '=', 'product.product_brand')
            ->join('factory', 'factory.factory_id', '=', 'product.factory')
            ->join('storage', 'storage.storage_id', '=', 'product.storage')
            ->join('category_spec', 'category_spec.id', '=', 'product.category')
            ->where('is_index', 1)
            ->orderBy('created', 'desc')
            ->skip($limit)->take(10)
            ->get();

    }


    /**
     * 产品数量
     * @param $product_id
     * @author xf
     * @date 2016-9-5
     * @return mixed
     */
    public function GetProduct($product_id)
    {
        return  DB::table($this->table)
            ->select('useable_stock','product_price','product_id','real_stock','useable_stock','locking_stock')
            ->where(['product.product_id' => $product_id])
            ->first();
    }
    // /**
    //  * 根据ID获取产品
    //  * @param $product_id
    //  * @return mixed
    //  */
    // public function APProductById($product_id)
    // {
    //     return  DB::table($this->table)
    //         ->selectRaw('sc_product.product_id,sc_brand.brand_name as product_brand,sc_product.product_spec,sc_product.product_price,sc_storage.storage_name as storage,sc_factory.factory_name as factory,sc_material.m_name as product_material,sc_product.useable_stock,sc_product.piece_weight')
    //         ->leftjoin('brand', 'product.product_brand', '=', 'brand.brand_id')
    //         ->leftjoin('material', 'product.product_material', '=', 'material.m_id')
    //         ->leftjoin('storage', 'product.storage', '=', 'storage.storage_id')
    //         ->leftjoin('factory', 'product.factory', '=', 'factory.factory_id')
    //         ->where('product.product_id',$product_id)
    //         ->first();
    // }



}